/*
 heshencao@163.com
 */
var ErrData = [];
var btUtils=new buttonFactory();//按钮操作工具类;

/**验证上传的文件名,如果出错返回False, */
function validateExcel(){
	try {
		var name = Ext.getCmp("excelFile").getValue()
		var as = name.split(".")
		if (as.length>1 && "xls" == as[as.length-1]) {
			return true;
		}
		else{
			Ext.Msg.alert("Excel出错", "Excel文件名出错.请注意系统只支持Excel2003及更老版本的Excellent文件!");
			return false;
		}
		
	} 
	catch (e) {
		return false;
	}
}

function uploadAction(){
    if (fileUploadPanel.getForm().isValid()) {
        fileUploadPanel.getForm().submit({
            url: 'uploadExcel.do?method=uploadExcel',
            waitMsg: '请稍等， 正在上传你的文件!...',
            failure: function(form, action){
                msg("验证", getFileName() + "数据验证完成!");
                var date = new Date();
				var isfailer=false;
                var time = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
                Ext.each(action.result, function(rd){
                    rd.time = time;
                    rd.opt = "上传数据"
					if (rd.type.lastIndexOf("_err") > 0) {
						isfailer=true;
					}
                    ErrData.push(rd);
                });
                grid.getStore().loadData(ErrData);
				if(isfailer){
					msg("验证失败", getFileName() + ",数据验证有错误信息!请对照下面的错误信息检查数据!");
				}else{
               		btUtils.enableButton('bt_preview', 'bt_showSubmitPanel')
					activeOptionPanel();
				}
            },
            success: function(form, action){
                msg("验证", getFileName() + ",数据验证出错!请联系管理员!");
            }
        });
    }
}

var bt_upload=btUtils.createButton({
	id:'bt_upload',
    text: '上传数据'
});

//为按钮添加一个弹出确认窗口;
function confirmAction(bt,fun){
	return function(){
		Ext.MessageBox.confirm('确认', "你确认要执行动作: <b style='color:red'>" + bt.text + "</b>,请确认!", function(btn){
			if (btn == 'yes') {
				fun(arguments)
			}
		})
	}
}


//初始化按钮事件;
btUtils.addListener('bt_upload','click',validateExcel);//验证Excellent文件;//btUtils.addListener('bt_submitDate','click',function(bt,e){})//写入数据库
btUtils.addListener('bt_upload','click',confirmAction(bt_upload,uploadAction));//上传


//初始化按钮状态;
btUtils.enableButton('bt_upload')
btUtils.disableButton('bt_preview','bt_showSubmitPanel')


/**获得文件名 */
function getFileName(){
    return "<span style='color:red;font-weight:bold;'>" + Ext.getCmp('excelFile').getValue() + "</span>";
}

var fileUploadPanel = new Ext.FormPanel({
    fileUpload: true,
    header : false,
    width: "60%",
    height: 180,
    frame: true,
    title: 'Excel文件上传处理',
    autoHeight: true,
    buttonAlign: 'center',
    labelWidth: 80,
    items: [{
        xtype: 'fileuploadfield',
        id: 'excelFile',
        emptyText: '请选择上传的Excel文件',
        fieldLabel: '上传Excel',
//        name: 'excelFile',
        width: 220,
        buttonCfg: {
            iconCls: 'upload-icon'
        }
    },{
		
	}],
  buttons: [bt_upload,'->']
});



function initPage(){

    Ext.QuickTips.init();
    
    var vp = new Ext.Viewport({
        id: 'specVpt',
        layout: 'fit',
        items: [{
            region: 'north',
            split: true,
            border: false,
            height: 95,
            useSplitTips: true,
            layout: 'fit',
            items: [fileUploadPanel]
        }]
    });
}

Ext.onReady(initPage);
